package com.hawk.eye.server.controller.hawkeye;

import com.hawk.eye.api.webhook.IWebhookRequester;
import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.framework.plugins.webhook.WebhookFactory;
import com.hawk.eye.server.convert.OuterWebhookConvert;
import com.hawk.eye.server.domain.dto.resp.OuterWebhookRespDTO;
import com.hawk.eye.server.domain.vo.req.OuterWebhookReqVO;
import com.hawk.eye.server.domain.vo.resp.OuterWebhookRespVO;
import com.hawk.eye.server.service.IOuterWebhookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * webhook管理 Controller
 * @author: salad
 * @date: 2023/9/30
 **/
@Slf4j
@RestController
@RequestMapping("/hawk-eye/webhook")
@RequiredArgsConstructor
public class WebhookController {

    private final IOuterWebhookService webhookService;

    private final WebhookFactory webhookFactory;

    /**
     * 请求方式列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:webhook:add') or @ss.hasPermi('hawk-eye:webhook:edit')")
    @GetMapping(value = "/requester-identity-list")
    public ApiResult<List<String>> outerRequesterList() {
        ConcurrentHashMap<String, IWebhookRequester> webhooks = webhookFactory.getWebhooks();
        if (CollectionUtil.isEmpty(webhooks)) {
            return ApiResult.ok();
        }
        List<String> requesterIdentities = new ArrayList<>();
        webhooks.forEach(((key, requester) -> {
            requesterIdentities.add(key);
        }));
        return ApiResult.ok(requesterIdentities);
    }

    /**
     * webhook列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:webhook:query')")
    @GetMapping(value = "/list")
    public ApiResult<List<OuterWebhookRespVO>> webhookList() {
        List<OuterWebhookRespDTO> list = webhookService.webhookList();
        return ApiResult.ok(list.parallelStream()
                .map(OuterWebhookConvert.INSTANCE::toRespVO)
                .collect(Collectors.toList()));
    }

    /**
     * 分页查询webhook
     * @param pageQuery
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:webhook:query')")
    @GetMapping("/page")
    public PageResult<OuterWebhookRespVO> page(PageQuery pageQuery, OuterWebhookReqVO reqVO) {
        PageResult<OuterWebhookRespDTO> page = webhookService.page(pageQuery, OuterWebhookConvert.INSTANCE.to(reqVO));
        return PageResult.of(page, OuterWebhookConvert.INSTANCE::toRespVO);
    }

    /**
     * webhook详情
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:webhook:query')")
    @GetMapping("/detail/{id}")
    public ApiResult<OuterWebhookRespVO> detail(@PathVariable("id") Long id) {
        OuterWebhookRespDTO dto = webhookService.detail(id);
        return ApiResult.ok(OuterWebhookConvert.INSTANCE.toRespVO(dto));
    }

    /**
     * 保存webhook
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:webhook:add')")
    @PostMapping("/save")
    public ApiResult save(@RequestBody @Validated OuterWebhookReqVO reqVO) {
        boolean status = webhookService.save(OuterWebhookConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 修改webhook
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:webhook:edit')")
    @PutMapping("/update")
    public ApiResult update(@RequestBody @Validated OuterWebhookReqVO reqVO) {
        boolean status = webhookService.update(OuterWebhookConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 删除webhook
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:webhook:remove')")
    @DeleteMapping("/remove/{ids}")
    public ApiResult remove(@PathVariable Long[] ids) {
        return ApiResult.status(webhookService.removeBatchByIds(CollectionUtil.toList(ids)));
    }

}
